package archimede;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.Point2D;
import java.util.ArrayList;

public class Rel extends Polygon {
	/**
	 * pannello che la contiene
	 */
	private Pannello pan;

	/**
	 * direzione 0=up,1=down,2=right,3=left
	 */
	private int direction = -1;

	private final int up = 0;
	private final int down = 1;
	private final int right = 2;
	private final int left = 3;
	private final int degree45 = 4;
	private final int degree135 = 5;
	private final int degree225 = 6;
	private final int degree315 = 7;
	private final int none = 8;

	/**
	 * tipo 1=punto P , 2=m , 3=c
	 */
	private int type = -1;
	private final int p = 1;
	private final int m = 2;
	private final int c = 3;
	private final int t = 4;
	private final int q = 5;
	private final int n = 6;

	private RelazioneMod modello;
	ArrayList punti;

	public Rel(int offx, int offy, String n, Pannello p, int ide,
			int direction, int type) {
		this.direction = direction;
		this.type = type;
		this.punti = new ArrayList();
		/* 89 */this.modello = new RelazioneMod(n, ide);
		/* 90 */iniziaDisegno(offx, offy);
		/* 91 */this.modello.setNome(n);
		/* 92 */this.pan = p;
	}

	public RelazioneMod getModello() {
		/* 97 */return this.modello;
	}

	public int getId() {
		/* 102 */return this.modello.getId();
	}

	public void setNome(String n) {
		/* 107 */this.modello.setNome(n);
	}

	public String getNome() {
		/* 112 */return this.modello.getNome();
	}

	public Point2D.Double getPosizione(int p) {
		return new Point2D.Double(this.xpoints[0], this.ypoints[0]);
	}

	public void eliminami() {

		Rel questa = this;
		this.pan.rimuovi(questa);
	}

	public void disegnami(Graphics2D g2) {
		int size = 15;
		int x = this.xpoints[1];
		int y = this.ypoints[0];
		if (type == p)
			g2.setColor(new Color(255, 129, 65));
		else if (type == m)
			g2.setColor(Color.YELLOW);
		else if (type == c) {
			g2.setColor(new Color(0, 193, 194));
			size = 15;
		} else if (type == t) {
			g2.setColor(new Color(0, 160, 0));
			size = 12;
		} else if (type == q) {
			g2.setColor(new Color(255, 239, 206));
			size = 15;
		} else if (type == n) {
						
			g2.setColor(Color.BLACK);
			size = 18;
			if (direction == up)
				g2.drawOval(x - 10, y , 50, 50);
			else if (direction == down)
				g2.drawOval(x - 40, y -50, 50, 50);
			else if (direction == right)
				g2.drawOval(x - 38, y - 10, 50, 50);
			else if (direction == left)
				g2.drawOval(x - 20, y - 25, 50, 50);
			else if (direction == degree45)
				g2.drawOval(x - 32, y - 12, 50, 50);
			else if (direction == degree135)
				//y -20 x=-10
				g2.drawOval(x - 18, y - 8, 50, 50);
			else if (direction == degree225)
				//y -20 x=+10
				g2.drawOval(x - 34, y - 43, 50, 50);
			else if (direction == degree315)
				g2.drawOval(x - 39, y - 35, 50, 50);
			
			
			g2.setColor(Color.RED);
		}
		g2.fill(this);
		g2.setColor(Color.BLACK);
		g2.draw(this);
		g2.setFont(new Font("TimesRoman", Font.PLAIN, size));

		g2.setColor(Color.BLACK);
		if (type < 6) {
			if (direction == up)
				g2.drawString(getNome(), x + 1, y + 30);
			else if (direction == down)
				g2.drawString(getNome(), x +1, y +15);
			else if (direction == right)
				g2.drawString(getNome(), x + 1, y + 25);
			else if (direction == left)
				g2.drawString(getNome(), x + 1, y + 7);
			else if (direction == degree45)
				g2.drawString(getNome(), x + 1, y + 23);
			else if (direction == degree135)
				g2.drawString(getNome(), x + 1, y + 25);
			else if (direction == degree225)
				g2.drawString(getNome(), x + 1, y + 23);
			else if (direction == degree315)
				g2.drawString(getNome(), x + 1, y + 23);
			else if (direction == none)
				g2.drawString(getNome(), x + 1, y + 23);
		} else {
			if (direction == up)
				g2.drawString(getNome(), x + 10, y + 40);
			else if (direction == down)
				g2.drawString(getNome(), x -20 , y - 23);
			else if (direction == right)
				g2.drawString(getNome(), x -25, y + 20);
			else if (direction == left)
				g2.drawString(getNome(), x + 1, y + 7);
			else if (direction == degree45)
				g2.drawString(getNome(), x -20, y + 23);
			else if (direction == degree135)
				g2.drawString(getNome(), x + 5, y + 25);
			else if (direction == degree225)
				g2.drawString(getNome(), x - 10, y - 15);
			else if (direction == degree315)
				g2.drawString(getNome(), x -25, y - 10);
		}
	}

	public void ridisegnami() {
		this.pan.repaint();
	}

	public void trascina(int x, int y) {
		translate(x, y);

	}

	/**
	 * disegna il polygono
	 * 
	 * @param offx
	 *            coordinata x
	 * @param offy
	 *            coordinata y
	 */
	public void iniziaDisegno(int offx, int offy) {
		reset();

		if (type < 6) {
			if (direction == up) {
				this.punti.add(new Point2D.Double(20 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(0 + offx, 10 + offy));
				this.punti.add(new Point2D.Double(0 + offx, 35 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 35 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 10 + offy));
			} else if (direction == down) {
				this.punti.add(new Point2D.Double(0 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(0 + offx, 25 + offy));
				this.punti.add(new Point2D.Double(20 + offx, 35 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 25 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 0 + offy));
			} else if (direction == right) {
				this.punti.add(new Point2D.Double(0 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(0 + offx, 30 + offy));
				this.punti.add(new Point2D.Double(30 + offx, 30 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 15 + offy));
				this.punti.add(new Point2D.Double(30 + offx, 0 + offy));
			} else if (direction == left) {
				this.punti.add(new Point2D.Double(0 + offx, 15 + offy));
				this.punti.add(new Point2D.Double(10 + offx, 30 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 30 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(10 + offx, 0 + offy));
			} else if (direction == degree225) {
				this.punti.add(new Point2D.Double(5 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(5 + offx, 20 + offy));
				this.punti.add(new Point2D.Double(0 + offx, 35 + offy));
				this.punti.add(new Point2D.Double(25 + offx, 30 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 30 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 0 + offy));
			} else if (direction == degree135) {
				this.punti.add(new Point2D.Double(0 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(5 + offx, 15 + offy));
				this.punti.add(new Point2D.Double(5 + offx, 35 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 35 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 5 + offy));
				this.punti.add(new Point2D.Double(15 + offx, 5 + offy));
			} else if (direction == degree45) {
				this.punti.add(new Point2D.Double(0 + offx, 5 + offy));
				this.punti.add(new Point2D.Double(0 + offx, 35 + offy));
				this.punti.add(new Point2D.Double(35 + offx, 35 + offy));
				this.punti.add(new Point2D.Double(35 + offx, 15 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(25 + offx, 5 + offy));
			} else if (direction == degree315) {
				this.punti.add(new Point2D.Double(0 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(0 + offx, 30 + offy));
				this.punti.add(new Point2D.Double(25 + offx, 30 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 35 + offy));
				this.punti.add(new Point2D.Double(35 + offx, 20 + offy));
				this.punti.add(new Point2D.Double(35 + offx, 0 + offy));
			} else if (direction == none) {
				this.punti.add(new Point2D.Double(0 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(0 + offx, 30 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 30 + offy));
				this.punti.add(new Point2D.Double(40 + offx, 0 + offy));
			}
		} else {
			if (direction == up) {
				this.punti.add(new Point2D.Double(15 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(0 + offx, 20 + offy));
				this.punti.add(new Point2D.Double(15 + offx, 12 + offy));
				this.punti.add(new Point2D.Double(30 + offx, 20 + offy));
			} else if (direction == down) {
				this.punti.add(new Point2D.Double(15 + offx, 20 + offy));
				this.punti.add(new Point2D.Double(30 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(15 + offx, 8 + offy));
				this.punti.add(new Point2D.Double(0 + offx, 0 + offy));			
			} else if (direction == right) {
				this.punti.add(new Point2D.Double(0 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(8 + offx, 15 + offy));
				this.punti.add(new Point2D.Double(0 + offx, 30 + offy));
				this.punti.add(new Point2D.Double(20 + offx, 15 + offy));
			} else if (direction == left) {
				this.punti.add(new Point2D.Double(0 + offx, 15 + offy));
				this.punti.add(new Point2D.Double(20 + offx, 30 + offy));
				this.punti.add(new Point2D.Double(12 + offx, 15 + offy));
				this.punti.add(new Point2D.Double(20 + offx, 0 + offy));	
			} else if (direction == degree225) {							
				this.punti.add(new Point2D.Double(0 + offx, 25 + offy));
				this.punti.add(new Point2D.Double(25 + offx, 15 + offy));
				this.punti.add(new Point2D.Double(10 + offx, 15 + offy));
				this.punti.add(new Point2D.Double(5 + offx, 0 + offy));
			} else if (direction == degree135) {
				this.punti.add(new Point2D.Double(0 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(10 + offx, 25 + offy));
				this.punti.add(new Point2D.Double(10 + offx, 10 + offy));
				this.punti.add(new Point2D.Double(25 + offx, 5 + offy));
			} else if (direction == degree45) {
				this.punti.add(new Point2D.Double(0 + offx, 5 + offy));
				this.punti.add(new Point2D.Double(15 + offx, 10 + offy));
				this.punti.add(new Point2D.Double(15 + offx, 25 + offy));
				this.punti.add(new Point2D.Double(25 + offx, 0 + offy));
			} else if (direction == degree315) {
				this.punti.add(new Point2D.Double(0 + offx, 15 + offy));
				this.punti.add(new Point2D.Double(25 + offx, 25 + offy));
				this.punti.add(new Point2D.Double(20 + offx, 0 + offy));
				this.punti.add(new Point2D.Double(15 + offx, 15 + offy));
			}
		}

		for (int i = 0; i < this.punti.size(); i++) {
			int x = (int) ((Point2D.Double) this.punti.get(i)).getX();
			int y = (int) ((Point2D.Double) this.punti.get(i)).getY();
			addPoint(x, y);
		}
	}

	public void posiziona(int[] nx, int[] ny) {
		reset();
		for (int i = 0; i < nx.length; i++)
			addPoint(nx[i], ny[i]);
	}

}